<?php
/**
 * 新房
 * User: E 房cms www.fangcms.com
 * Date: 2015/1/20
 * Time: 10:51
 */
namespace Home\Controller;
use Org\Yike\FangCms;
use Org\Util\Page;
class HouseController extends CommonController{
    public function _initialize(){
        parent::_initialize();
        $this->action = strtolower(ACTION_NAME);
    }

    public function index(){
        $FangCms = new FangCms();
        $menu = getLinkMenuFor(array(2,3,4,5,6,8,9,10,18,19),1);
        $houseArr = $FangCms->getHouseSearch($menu,true);
        $this->rec_house = $FangCms->getRecHouse(8);
        $this->house_cate = $houseArr['arr']['house_cate'];
        $this->cate_pid_info = $houseArr['arr']['cate_pid_info'];
        $this->cate_pid_list = $houseArr['arr']['cate_pid_list'];
        $this->menu = $menu;
        $this->url = $houseArr['url'];
        $this->s = $houseArr['s'];
        $this->list = $houseArr['arr']['list'];
        $this->page = $houseArr['arr']['page'];
        $this->houseArr = $houseArr;
        if($this->cate_pid_info){
            $this->seoKey($this->cate_pid_info,'name');
        }
        $this->display();
    }

    //楼盘主页
    public function detail(){
        $resArr = $this->getHouse();
        $where = array('house_id'=>$resArr['house']['id'],'status'=>1);
        $menu = getLinkMenuFor(array(6,5));
        $this->tags = explode('|',trim($resArr['house']['tags'],'|'));
        $this->house_type =  M('house_type')->where($where)->field('id,name,area,room')->order('ordid desc')->limit(4)->select();

        //图片切换
        $pic_model = M('house_picture');
        $picCate = $menu[5];
        $newPic = array();
        foreach($picCate as $k=>$v){
            $pic = $pic_model->where(array('house_id'=>$resArr['house']['id'],'status'=>1,'cate_id'=>$k))->order('ordid desc')->getField('img');
            if($pic){
                $newPic[$k]['name'] = $v;
                $newPic[$k]['img'] = $pic;
            }
        }
        $this->newPic = $newPic;

        //新闻动态
        $this->news = M('article')->where($where)->field('id,title,desc,cate_id,add_time')->limit(2)->order('id desc')->select();
        $this->news_cate = cateFor(0,'article_cate');

        //在售户型
        $this->house_type_list = M('house_type')->where($where)->limit(5)->order('ordid desc')->select();

        //沙盘
        $this->property_img = M('property_img')->where(array('house_id'=>$resArr['house']['id']))->find();
        $this->property_data = unserialize($this->property_img['data']);

        //楼盘图片
        $this->house_pic = M('house_picture')->where($where)->limit(5)->order('id desc')->select();
        $houseTypeNav = getHouseType($resArr['house']['id'],1);
        $this->house_type_nav_list = current($houseTypeNav['house_type']);
        $this->house_type_nav  = $houseTypeNav;
        $this->menu = $menu;
        $this->numTurnCn = numTurnCn();
        $this->explain = _setting('explain');
        $this->seoKey($resArr['house']);
        $this->display();
    }

    //详细信息
    public function info(){
        $resArr = $this->getHouse();
        $where = array('house_id'=>$resArr['house']['id']);
        $house_data = M('house_data')->where($where)->find();
        $this->mating = unserialize($house_data['mating']);
        $this->data = unserialize($house_data['data']);
        $this->menu = getLinkMenuFor(array(4),1);
        $this->house_data =  $house_data;

        //价格信息
        $this->price_list = M('house_price')->where($where)->order('add_time desc')->limit(6)->select();

        //关键词设置
        $this->seoKey(array('seo_title'=>$resArr['house']['title'].'详细信息_'.$resArr['house']['title'].'配套信息'));
        $this->display();
    }

    //楼栋户型
    public function type(){
        $resArr = $this->getHouse();
        $where = array('status'=>1,'house_id'=>$resArr['house']['id']);
        if($cate = intval($_GET['cate'])){
            $where['room'] = $cate;
            $this->cate = $cate;
        }
        $count = M('house_type')->where($where)->count();
        $pager = new Page($count, 10);
        $this->list = M('house_type')->where($where)->limit($pager->firstRow.','.$pager->listRows)->order('id desc')->select();
        $this->page = $pager->show();
        $houseType = getHouseType($resArr['house']['id'],1);
        $this->house_type = $houseType['list'];
        unset($where['room']);
        $this->type_count = M('house_type')->where($where)->count();
        //关键词设置
        $title = $cate ? $cate.'居室' : '在售户型';
        $this->seoKey(array('seo_title'=>$resArr['house']['title'].$title));
        $this->display();
    }

    //楼栋户型详细
    public function typeShow(){
        $id = intval($_GET['id']);
        if(!$id){
            $this->error('参数错误');
        }
        $model = M('house_type');
        $resArr = $this->getHouse();
        $houseType = getHouseType($resArr['house']['id'],1);
        $this->house_type = $houseType['list'];
        $this->type_count = M('house_type')->where(array('status'=>1,'house_id'=>$resArr['house']['id']))->count();
        $this->type_info = $model->where(array('id'=>$id,'status'=>1))->find();
        //下
        $this->up_one = $model->where(array('id'=>array('gt',$id,'status'=>1,'house_id'=>$resArr['house']['id'])))->order('id asc')->find();
        //上
        $this->down_one = $model->where(array('id'=>array('lt',$id,'status'=>1,'house_id'=>$resArr['house']['id'])))->order('id desc')->find();
        //关键词设置
        $this->seoKey(array('seo_title'=>$resArr['house']['title'].$this->info['name']));
        $this->display();
    }

    //价格
    public function price(){
        $resArr = $this->getHouse();
        $where = array('status'=>1,'house_id'=>$resArr['house']['id']);
        $count = M('house_price')->where($where)->count();
        $pager = new Page($count, 16);
        $this->price_list = M('house_price')->where($where)->limit($pager->firstRow.','.$pager->listRows)->order('add_time desc')->select();
        $this->page = $pager->show();
        //关键词设置
        $this->seoKey(array('seo_title'=>$resArr['house']['title'].'价格走势'));
        $this->display();
    }

    //图片
    public function pic(){
        $resArr = $this->getHouse();
        $model = M('house_picture');
        $pic_count = array();
        $pic_count['count'] = $model->where(array('status'=>1,'house_id'=>$resArr['house']['id']))->count();
        $menu = getLinkMenuFor(array(5));
        foreach($menu[5] as $k=>$v){
            $pic_count[$k] = $model->where(array('status'=>1,'cate_id'=>$k,'house_id'=>$resArr['house']['id']))->count();
        }
        $this->pic_count = $pic_count;
        $where = array('status'=>1,'house_id'=>$resArr['house']['id']);
        if($cate = intval($_GET['cate'])){
            $where['cate_id'] = $cate;
            $this->cate = $cate;
        }
        $count = M('house_picture')->where($where)->count();
        $pager = new Page($count, 10);
        $this->list = M('house_picture')->where($where)->limit($pager->firstRow.','.$pager->listRows)->order('id desc')->select();
        $this->page = $pager->show();
        $this->menu = $menu;
        $title = $menu[5][$cate] ? $menu[5][$cate] : '楼盘相册';
        $this->seoKey(array('seo_title'=>$resArr['house']['title'].$title));
        $this->display();
    }

    //动态
    public function news(){
        $resArr = $this->getHouse();
        $where = array('status'=>1,'house_id'=>$resArr['house']['id']);
        $cate = intval($_GET['cate']);
        if($cate){
            $where['cate_id'] = $cate;
            $this->cate = $cate;
        }
        $count = M('article')->where($where)->count();
        $pager = new Page($count, 10);
        $this->list = M('article')->where($where)->limit($pager->firstRow.','.$pager->listRows)->order('id desc')->select();
        $this->page = $pager->show();
        $this->news_cate = $news_cate = cateFor(0,'article_cate');
        $this->news_cate_nav = M('article_cate')->where(array('status'=>1,'pid'=>4))->order('ordid desc')->limit(4)->select();

        //关键词设置
        $title = $news_cate[$cate] ? $news_cate[$cate].'新闻,信息,动态' : '资讯动态';
        $this->seoKey(array('seo_title'=>$resArr['house']['title'].$title));
        $this->display();
    }

    //问答
    public function ask(){
        $resArr = $this->getHouse();
        $where = array('status'=>1,'house_id'=>$resArr['house']['id']);
        $count = M('ask')->where($where)->count();
        $pager = new Page($count, 10);
        $this->list = M('ask')->where($where)->limit($pager->firstRow.','.$pager->listRows)->order('id desc')->select();
        $this->page = $pager->show();
        $FangCms = new FangCms();
        $this->ask_cate = $FangCms->getCate('ask_cate',1);
        $this->seoKey(array('seo_title'=>$resArr['house']['title'].'在线问答'));
        $this->display();
    }

    //30天数据图
    public function line(){
        if($d = $_POST['days']){
            $days = array(
                3=>60,
                6=>180,
                1=>0
            );
            $where = array('house_id'=>intval($_POST['house_id']));
            if($days[$d]){
                $new_days = date("Y-m-d",strtotime("-$days[$d] day"));
                $s_day = strtotime($new_days.' 00:00:00');
                $e_day = strtotime(date('Y-m-d').' 23:59:59');
                $where['add_time']  = array(array('egt',$s_day),array('elt',$e_day),'and');
            }
            $price_list = M('house_price')->where($where)->order('add_time asc')->select();
            if(count($price_list)){
                $num = array();
                $time = array();
                foreach($price_list as $key=>$val){
                    if($val['average_price'] && $val['add_time']){
                        $time[] = date("Y-m-d",$val['add_time']);
                        $num['max_price'][$key] = (int)$val['max_price'];
                        $num['average_price'][$key] = (int)$val['average_price'];
                        $num['min_price'][$key] = (int)$val['min_price'];
                    }
                }
                $str = array(
                    'code'=>1,
                    'num'=>$num,
                    'categories'=>$time
                );
            }else{
                $str = array( 'code'=>0);
            }
        }
        echo json_encode($str);
    }

    private function getHouse(){
        $house_id = intval($_GET['hid']) ? intval($_GET['hid']) : intval($_GET['id']);
        if(!$house_id){
            $this->error('参数不正确',U('House/index'));
        }
        $house = M('house')->where(array('id'=>$house_id))->find();
        if(!$house['id']){
            $this->error('找不到对应的信息',U('House/index'));
        }

        //给楼盘增加浏览次数
        $FangCms = new FangCms();
        $FangCms->setInc($house['id'],'house_info',array('house_id'=>$house['id']));

        //当前位置
        $this->nav = $FangCms->currentLocation('house_cate',array('status'=>1,'id'=>$house['area_id']));

        if($this->action=='detail'){
            //推荐楼盘
            $this->rec_house = $FangCms->getRecHouse(5);
            //热门楼盘
            $this->hits_house = $FangCms->getHitsHouse(16);
            //附近楼盘
            $this->nearby_house = M('house')->where(array('status'=>1,'area_id'=>$house['area_id']))->field('id,title')->order('rand()')->limit(20)->select();

        }

        if($this->action == 'news' or $this->action=='ask'){
            //推荐楼盘
            $this->rec_house = $FangCms->getRecHouse(6);
            //附近楼盘
            $this->nearby_house = M('house')->where(array('status'=>1,'area_id'=>$house['area_id']))->field('id,title')->order('rand()')->limit(6)->select();
            //最新新闻
            $this->new_news = M('article')->where(array('status'=>1))->limit(10)->order('id desc')->select();
        }

        $info = M('house_info')->where(array('house_id'=>$house['id']))->find();

        $this->address = explode('|',$house['address']);
        $tel = explode('|',$house['tel']);
        if(!$tel[0]){
            $contact = _setting('contact');
            $tel = explode('|',$contact['universal_phone']);
        }
        $this->tel = $tel;
        $this->house = $house;
        $this->info = $info;
        return array('house'=>$house,'info'=>$info);
    }
}